package com.costeaalex.MIDlets.remote.lists;

import java.io.IOException;
import java.util.Vector;

import javax.bluetooth.RemoteDevice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.costeaalex.MIDlets.remote.BlueMIDlet;

public class DeviceList implements CommandListener
	{
	
	private BlueMIDlet	midlet;
	public List deviceList;
	private Command cmExit;
	private Command cmSelect;
	//private Command cmItem;
	//private Command cmOK;
	private Vector devicesList;
	
	public DeviceList(Vector devices, BlueMIDlet midl)
		{
		int i;
		midlet=midl;
		devicesList=devices;
		deviceList = new List("Devices Found", List.IMPLICIT);
		for(i=0; i<devices.size(); i++)
			try
				{
				deviceList.insert(i, ((RemoteDevice) devices.elementAt(i)).getFriendlyName(true), null);
				}
			catch (IOException e)
				{
				e.printStackTrace();
				}
		
		//cmOK=new Command("OK", -5, 2);
		//cmItem=new Command("Item", Command.ITEM, 3);
		cmExit=new Command("Exit", Command.EXIT, 1);
		cmSelect=new Command("Select", Command.SCREEN, 2);
		
		deviceList.addCommand(cmExit);
		deviceList.addCommand(cmSelect);
		deviceList.setCommandListener(this);
		}
	//midlet.deviceSelected(selected);

	public void commandAction(Command c, Displayable d)
		{
		
		//deviceList.append(c.getLabel(), null);
		
		if(c==cmExit)
			{
			midlet.shutDown();
			}
						
		if(c==cmSelect)
			{
			midlet.deviceSelected((RemoteDevice) devicesList.elementAt(deviceList.getSelectedIndex()));
			}
		}
	}
